package com.ruoyi.crm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.crm.domain.ContactInfo;
import com.ruoyi.crm.domain.CustomerInfo;
import com.ruoyi.crm.domain.ProjectApprove;
import com.ruoyi.crm.service.IContactService;
import com.ruoyi.crm.service.ICustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 联系人控制层
 * @author  dxy
 *
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/contact")

public class ContactController extends BaseController {

    private final IContactService contactService;
    private final ICustomerService customerService;

    /**
     *
     *查询联系人列表
     */

    @SaCheckPermission("crm:contact:list")
    @GetMapping("/list")
    public TableDataInfo<ContactInfo> list(ContactInfo contactInfo, PageQuery pageQuery) {
        //分页查询方法一：对表操作，对表进行遍历
        //（1）先查询整个列表
        TableDataInfo<ContactInfo> list= contactService.selectPageContactInfoList(contactInfo, pageQuery);
        //（2）遍历整个表
        list.getRows().forEach(item->{
            if(item.getContactId()!=null){
                //判断id是否存在
                CustomerInfo customerInfo=customerService.selectCustomerById(item.getCustomerId());
                if(customerInfo!=null){
                    //在customerInfo中取到getCustomerName()
                    item.getParams().put("customerName",customerInfo.getCustomerName());
                }

            }
        });
        return list;
    }
    /**
     * 根据联系人ID获取详细信息
     *
     * @param contactId 联系人ID
     */
    @SaCheckPermission("crm:contact:query")
    @GetMapping(value = "/{contactId}")
    public R<ContactInfo> getInfo(@PathVariable Long contactId) {
        return R.ok(contactService.selectContactById(contactId));
    }

    /**
     *新增联系人
     */

    @SaCheckPermission("crm:contact:add")
    @Log(title = "联系人", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ContactInfo contactInfo) {
        return toAjax(contactService.insertContact(contactInfo));
    }

    /**
     *修改联系人
     */
    @SaCheckPermission("crm:contact:edit")
    @Log(title = "联系人",businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ContactInfo contactInfo) {
        return toAjax(contactService.updateContact(contactInfo));
    }

    /**
     * 删除联系人
     */
    @SaCheckPermission("crm:contact:remove")
    @Log(title = "联系人",businessType = BusinessType.DELETE)
    @DeleteMapping("/{contactId}")
    public R<Void> remove(@PathVariable Long contactId) {

        return toAjax(contactService.delectContact(contactId));
    }

    /**
     * 查询联系人列表
     */
    @GetMapping("/ContactList")
    public R<List<ContactInfo>> getContactList() {
        List<ContactInfo> list=contactService.selectContactList(new ContactInfo());
        return R.ok(list);
    }

}
